package game;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class MyFrame extends JFrame implements KeyListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	int rectX, rectY;

	int oldX, oldY;

	int width = 500;

	int height = 500;

	public MyFrame() {
		super();
		this.setSize(this.width, this.height);
		this.setVisible(true);

		this.repaint();
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		rectX = width / 2 - 10;
		rectY = height / 2 - 10;
		oldX = rectX;
		oldY = rectY;

		this.addKeyListener(this);
	}

	public void keyPressed(KeyEvent arg0) {
		switch (arg0.getKeyCode()) {
		case KeyEvent.VK_UP: {
			rectY -= 10;
			repaint();
			break;
		}
		case KeyEvent.VK_DOWN: {
			rectY += 10;
			repaint();
			break;
		}
		case KeyEvent.VK_RIGHT: {
			rectX += 10;
			repaint();
			break;
		}
		case KeyEvent.VK_LEFT: {
			rectX -= 10;
			repaint();
			break;
		}
		}

	}

	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void paint(Graphics g) {
		g.clearRect(oldX, oldY, 20, 20);
		g.fillRect(rectX, rectY, 20, 20);
		oldX = rectX;
		oldY = rectY;
	}

}
